/*======================================================================
 
 UNLESS OTHERWISE AGREED TO IN A SIGNED WRITING BY HONEYWELL INTERNATIONAL INC
 (“HONEYWELL”) AND THE USER OF THIS CODE, THIS CODE AND INFORMATION IS PROVIDED
 "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING
 BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
 FOR A PARTICULAR PURPOSE.
 
 COPYRIGHT (C) 2008 HONEYWELL INTERNATIONAL INC.
 
 THIS SOFTWARE IS PROTECTED BY COPYRIGHT LAWS OF THE UNITED STATES OF
 AMERICA AND OF FOREIGN COUNTRIES. THIS SOFTWARE IS FURNISHED UNDER A
 LICENSE AND/OR A NONDISCLOSURE AGREEMENT AND MAY BE USED IN ACCORDANCE
 WITH THE TERMS OF THOSE AGREEMENTS. UNAUTHORIZED REPRODUCTION,  DUPLICATION
 OR DISTRIBUTION OF THIS SOFTWARE, OR ANY PORTION OF IT  WILL BE PROSECUTED
 TO THE MAXIMUM EXTENT POSSIBLE UNDER THE LAW.
 
 ======================================================================*/
//
//  PMVC.m
//  SDKTest
//

#import "PMVC.h"

@interface PMVC ()

@property (assign,nonatomic)BOOL updating;
-(void)updateLabels;

@end

@implementation PMVC
@synthesize chargeStatusLabel;
@synthesize batteryStatusLabel;
@synthesize updating;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
      self.title = @"PM Test";
   }
   return self;
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   self.updating = NO;

    [[Captuvo sharedCaptuvoDevice] addCaptuvoDelegate:self];
    
    [[Captuvo sharedCaptuvoDevice] startPMHardware] ;
    
   [self updateLabels];
   // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{

}
- (void)viewDidUnload
{
   [self setChargeStatusLabel:nil];
   [self setBatteryStatusLabel:nil];
   [super viewDidUnload];
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
   self.updating = NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)updateLabels{
   ChargeStatus chargeStatus = [[Captuvo sharedCaptuvoDevice]getChargeStatus];
   BatteryStatus BatteryStatus = [[Captuvo sharedCaptuvoDevice]getBatteryStatus];
   switch (chargeStatus) {
      case ChargeStatusCharging:
         self.chargeStatusLabel.text=@"Charging";
         break;
      case ChargeStatusUndefined:
         self.chargeStatusLabel.text=@"Undefined";
         break;
      case ChargeStatusChargeComplete:
         self.chargeStatusLabel.text=@"Charge complete";
         break;
      case ChargeStatusNotCharging:
         self.chargeStatusLabel.text=@"Not charging";
         break;
      default:
         break;
   }
   switch (BatteryStatus) {
      case BatteryStatusPowerSourceConnected:
         self.batteryStatusLabel.text = @"|*AC*|";
         break;
      case BatteryStatus4Of4Bars:
         self.batteryStatusLabel.text = @"|****|";
         break;
      case BatteryStatus3Of4Bars:
         self.batteryStatusLabel.text = @"|*** |";
         break;
      case BatteryStatus2Of4Bars:
         self.batteryStatusLabel.text = @"|**  |";
         break;
      case BatteryStatus1Of4Bars:
         self.batteryStatusLabel.text = @"|*   |";
         break;
      case BatteryStatus0Of4Bars:
         self.batteryStatusLabel.text = @"|    |";
         break;
      case BatteryStatusUndefined:
         self.batteryStatusLabel.text = @"UNDEFINE";
      default:
         break;
   }
   if (self.updating) {
      double delayInSeconds = 2.0;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
         [self updateLabels];
      });
   }
   
}

-(void)pmBatteryStatusChange:(BatteryStatus)newBatteryStatus{
   switch (newBatteryStatus) {
      case BatteryStatusPowerSourceConnected:
         self.batteryStatusLabel.text = @"|*AC*|";
         break;
      case BatteryStatus4Of4Bars:
         self.batteryStatusLabel.text = @"|****|";
         break;
      case BatteryStatus3Of4Bars:
         self.batteryStatusLabel.text = @"|*** |";
         break;
      case BatteryStatus2Of4Bars:
         self.batteryStatusLabel.text = @"|**  |";
         break;
      case BatteryStatus1Of4Bars:
         self.batteryStatusLabel.text = @"|*   |";
         break;
      case BatteryStatus0Of4Bars:
         self.batteryStatusLabel.text = @"|    |";
         break;
      case BatteryStatusUndefined:
         self.batteryStatusLabel.text = @"UNDEFINE";
      default:
         break;
   }
}

-(void)pmChargeStatusChange:(ChargeStatus)newChargeStatus{
   switch (newChargeStatus) {
      case ChargeStatusCharging:
         self.chargeStatusLabel.text=@"Charging";
         break;
      case ChargeStatusUndefined:
         self.chargeStatusLabel.text=@"Undefined";
         break;
      case ChargeStatusChargeComplete:
         self.chargeStatusLabel.text=@"Charge complete";
         break;
      case ChargeStatusNotCharging:
         self.chargeStatusLabel.text=@"Not charging";
         break;
      default:
         break;
   }
}
- (IBAction)forceShutdown:(id)sender {
   [[Captuvo sharedCaptuvoDevice] forceBatteryLowShutdown];
}

- (void)pmReady
{
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil
                                                   message:@"pm ready"
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [alert show];
    
    
}

@end
